package tm.actions;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import tm.data.DaoFactory;
import tm.data.ProjectsDao;
import tm.dto.Projects;
import tm.dto.Roles;
import tm.dto.Users;
import tm.gui.ProjectsForm;

public class ProjectsAction extends Action{


	    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	            throws Exception {

	        ActionMessages errors = new ActionMessages();
	        ActionForward forward = new ActionForward(); // return value

	        try {

	        	ProjectsForm projData = (ProjectsForm)form;
	        	ProjectsDao projDao = DaoFactory.getInstance().getProjectsDao();
	        	Projects proj = new Projects();
	        	proj.setProjectId(projData.getProjectId());
	        	proj.setProjectName(projData.getProjectName());
	        	proj.setProjectDescription(projData.getProjectDescription());
	        	projDao.saveOrUpdate(proj);
	        		
	        } catch (Exception e) {
	        	System.out.println("User does not exist..Or password does not match..");
	        	errors.add("name", new ActionMessage("id"));
	        }
	        
	        if (!errors.isEmpty()) {
	            saveErrors(request, errors);

	            // Forward control to the appropriate 'failure' URI (change name as desired)
	            forward = mapping.findForward("failure");

	        } else {

	            // Forward control to the appropriate 'success' URI (change name as desired)
	            forward = mapping.findForward("success");

	        }

	        // Finish with
	        return (forward);

	    }


}
